package view;

import dao.UserDao;
import model.User;
import util.DbUtil;
import util.toolUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

public class LoginFrm  extends JFrame {



    public static User currentUser;

    private JFrame jf;

    private JTextField userNameText;

    private JTextField passwordText;

    private JComboBox  comboBox;



    private UserDao userDao;

    private DbUtil dbUtil;






        public LoginFrm(){

            jf=new JFrame("图书管理");

            jf.getContentPane().setFont(new Font("幼圆",Font.BOLD,14));

            jf.setBounds(700,250,500,467);

            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            jf.getContentPane().setLayout(null);




            JLabel lblNewLabel =new JLabel(
                    new ImageIcon("") );

            // lblNewLabel.setBounds(120,10,230,218);

            jf.getContentPane().add(lblNewLabel);



            JLabel label= new JLabel("用户名:");

            label.setFont(new Font("幼圆",Font.BOLD,14));

            label.setBounds(129,100,60,29);

            jf.getContentPane().add(label);







            userNameText =new JTextField();

            userNameText.setBounds(199,100,127,25);

            jf.getContentPane().add(userNameText);






            userNameText.setColumns(10);

            JLabel label_1 =new JLabel("密码:");

            label_1.setFont(new Font("幼圆",Font.BOLD,14));

            label_1.setBounds(144,190,45,29);

            jf.getContentPane().add(label_1);









            passwordText =new JPasswordField();
            passwordText.setColumns(10);
            passwordText.setBounds(199,190,127,25);
            jf.getContentPane().add(passwordText);







            JLabel label_2=new JLabel("权限:");
            label_2.setFont(new Font("幼圆",Font.BOLD,14));
            label_2.setBounds(144,280,45,29);
            jf.getContentPane().add(label_2);







            comboBox=new JComboBox();
            comboBox.setBounds(199,280,127,25);
            comboBox.addItem("用户");
            comboBox.addItem("管理员");
            jf.getContentPane().add(comboBox);







            JButton button =new JButton("登录");
            button.setBounds(153,330,65,29);
            jf.getContentPane().add(button);


            JButton button1=new JButton("注册");
            button1.setBounds(250,330,65,29);
            jf.getContentPane().add(button1);




            jf.setVisible(true);










                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {


                        checkLogin(e);

                    }
                });



                button1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {


                        register(e);

                    }
                });

    }





    private void register(ActionEvent e) {



            jf.dispose();

            new RegFrm();



    }


    private void checkLogin(ActionEvent e) {

            userDao=new UserDao();
            dbUtil=new DbUtil();


        String userName = userNameText.getText();

        String password = passwordText.getText();

        int index = comboBox.getSelectedIndex();

        if(toolUtil.isEmpty(userName)||toolUtil.isEmpty(password)){
            JOptionPane.showMessageDialog(null,"用户名和密码不能为空");
            return;
        }


        User user =new User();
        user.setUserName(userName);
        user.setPassword(password);
        if(index==0){
            user.setRole(1);
        }else{
            user.setRole(2);
        }


        Connection con =null;


        try {
            con = dbUtil.getConnection();
            User login = userDao.login(con, user);
            currentUser = login;

            if (login == null) {

                JOptionPane.showMessageDialog(null, "登录失败");
            } else {
                if (index == 0) {

                    jf.dispose();


                    //用户菜单Frame
                    new UserMenuFrm();
                    System.out.println("登录成功");



                } else {
                    jf.dispose();


                    //管理员Frame
                    new AdminFrame();

                    System.out.println("登陆成功");




                }

            }
        }catch (Exception e21) {

            e21.printStackTrace();

            JOptionPane.showMessageDialog(null,"登录异常");

        }finally {


            try {
                dbUtil.closeCon(con);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }


        }











      }








    }







